package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.ClassShopBeen;
import cn.xfdzx.android.apps.shop.bean.LiveListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.MyGridView;
import cn.xfdzx.android.apps.shop.view.TagTextView;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：视频列表
 */
public class HotVideoAdapter extends RecyclerView.Adapter<HotVideoAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<LiveListBeen.DataBean.ListsBean> mList;
    private Context context;


    public HotVideoAdapter(Context context, List<LiveListBeen.DataBean.ListsBean> list) {
        this.mList = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_hot, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        if (null!=mList.get(position).getLive().getCover()) {
            Glide.with(context).load(mList.get(position).getLive().getCover()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivImage);
        }
        if (null!=mList.get(position).getUser()) {
            if (null != mList.get(position).getUser().getAvatar()) {
                Glide.with(context).load(mList.get(position).getUser().getAvatar()).apply(options.transform(new CircleCrop(), roundedCorners)).into(holder.ivLogo);
            }
            holder.tvNickname.setText(mList.get(position).getUser().getNickname());
        }
        if (null!=mList.get(position).getGoods()){
        if (mList.get(position).getGoods().size() != 0) {
            Glide.with(context).load(mList.get(position).getGoods().get(0).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivGoodImage1);
            if (Double.parseDouble(mList.get(position).getGoods().get(0).getDiscountprice())==0) {
                holder.tvPrice1.setText("¥"+mList.get(position).getGoods().get(0).getMarketprice());
            }else {
                holder.tvPrice1.setText("¥"+mList.get(position).getGoods().get(0).getDiscountprice());
            }
            if (mList.get(position).getGoods().size() != 1) {
                Glide.with(context).load(mList.get(position).getGoods().get(1).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivGoodImage2);
                if (Double.parseDouble(mList.get(position).getGoods().get(1).getDiscountprice())==0) {
                    holder.tvPrice2.setText("¥"+mList.get(position).getGoods().get(1).getMarketprice());
                }else {
                    holder.tvPrice2.setText("¥"+mList.get(position).getGoods().get(1).getDiscountprice());
                }
            }}
        }

        holder.tvTitle.setText(mList.get(position).getLive().getTitle());

        // ImageGlideUtil.loadImage(context,holder.iv_good_image1,mList.get(position).getGoods().get(0).getThumb());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });


    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_price_1)
        TextView tvPrice1;
        @BindView(R.id.tv_price_2)
        TextView tvPrice2;
        @BindView(R.id.tv_nickname)
        TextView tvNickname;
        @BindView(R.id.iv_good_image1)
        ImageView ivGoodImage1;
        @BindView(R.id.iv_good_image2)
        ImageView ivGoodImage2;
        @BindView(R.id.iv_logo)
        ImageView ivLogo;
        @BindView(R.id.iv_image)
        ImageView ivImage;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
